﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using PlanProd.Contracts;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class DestinoEdit : BasePage
    {
        public Destino destinoCurrent
        {
            get
            {
                return (Destino)ViewState["destino"];
            }
            set
            {
                ViewState["destino"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //DESTINO_CLAVE_SEGURIDAD_EDITAR;
            HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);

            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["Id"] == null)
                {
                    destinoCurrent = new Destino();
                }
                else
                {
                    destinoCurrent = WebServiceHelper.GetDestinoById(Convert.ToInt32(this.Request.QueryString["Id"]));
                }

                Bind();
            }
        }

        protected override void ApplySecurity()
        {
            this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            this.txtSecurityKey.ReadOnly = !HasPermissionFor(PlanProdSecurity.DESTINO_CLAVE_SEGURIDAD_EDITAR);

            if (this.txtSecurityKey.ReadOnly)
            {
                this.txtSecurityKey.CssClass += " readonly";
            }
        }

        private void Bind()
        {
            if (destinoCurrent.Id != 0)
            {
                this.txtDestinoID.Text = Convert.ToString(destinoCurrent.Id);
                this.txtNombre.Text = destinoCurrent.Nombre;
                this.txtCodigoExterno.Text = destinoCurrent.CodigoExterno;
                this.txtSecurityKey.Text = destinoCurrent.SecurityKey;
                this.txtMapeoDestinoExterno.Text = destinoCurrent.MapeoDestinoExterno;
                BindAuditory(destinoCurrent);
            }
        }

        private void SetData()
        {
            destinoCurrent.EmpresaId = BaseWebPage.CompanyId;
            destinoCurrent.Nombre = this.txtNombre.Text;
            destinoCurrent.CodigoExterno = this.txtCodigoExterno.Text;
            destinoCurrent.SecurityKey = this.txtSecurityKey.Text;
            destinoCurrent.MapeoDestinoExterno = this.txtMapeoDestinoExterno.Text;
        }

        private void Save()
        {
            destinoCurrent = WebServiceHelper.SaveDestino(destinoCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect("/App/DestinoList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/App/DestinoList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}